﻿using System;
using System.Web;
using INHollandair.Core.Models;
using INHollandair.Core.Services;

namespace INHollandair.Shop
{
    public partial class CancelOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string orderUrl = (Request.QueryString["OrderUrl"] != null) ? Server.HtmlEncode(Request.QueryString["OrderUrl"]) : String.Empty;

            if (Session["OrderCancel"] != null)
            {
                MultiViewOrderCancel.SetActiveView(validUser);
                Session["OrderCancel"] = null;
            }
            else if (!orderUrl.Equals(string.Empty))
            {
                OrderService svc = new OrderService();
                Order url = svc.GetOrderByUrl(orderUrl);

                if (url != null)
                {
                    //Retrieve order
                    Order newOrder = svc.GetOrderByUrl(orderUrl);

                    if (HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        //Retrieve user
                        UserService userService = new UserService();
                        User user = userService.GetUserByName(HttpContext.Current.User.Identity.Name);

                        //Check if logged in user is the same as the user that ordered the product
                        if (newOrder.UserID.Equals(user.Id))
                        {
                            if (!newOrder.confirmed)
                            {
                                //Delete order
                                OrderService orderService = new OrderService();
                                orderService.RemoveOrder(newOrder);

                                //Forward user
                                Session["OrderCancel"] = true;
                                Response.Redirect("~/Shop/CancelOrder.aspx");
                            }
                            else
                            {
                                //feedback dat bevestigde producten niet gecancelled kunnen worden.
                                MultiViewOrderCancel.SetActiveView(orderConfirmed);
                            }
                        }
                        else
                        {
                            //Show message that the user isn't the owner
                            MultiViewOrderCancel.SetActiveView(invalidUser);
                        }
                    }
                    else
                    {
                        Response.Redirect(String.Format("~/Account/Login.aspx?ReturnUrl=%2fShop%CancelOrder.aspx?OrderUrl={0}", url.OrderURL));
                    }
                }
                else
                {
                    Response.Redirect("~/Default.aspx"); //gebruiker zit te prutsen met strings
                }
            }
            else
            {
                Response.Redirect("~/Default.aspx"); //gebruiker heeft hier niks te zoeken
            }
        }
    }
}